<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class FaqsController extends AppController {

	public $uses = array('News', 'Category', 'Faqs');
	/**
	 * Components
	 * @var array
	 */
	public $components = array(
		'Paginator',
		'ConstAdmin',
		'Common',
		'Session',
		'Captcha',
	);
	public $helpers = array ('Html', 'Form', 'Captcha');

	/**
	 * Displays a view
	 *
	 * @param mixed What page to display
	 * @return void
	 * @throws NotFoundException When the view file could not be found
	 *	or MissingViewException in debug mode.
	 */
	public function index($cat_rewrite) {
		$sercuritySalt = Configure::read('Security.salt');
		$cat_rewrite = md5(md5($cat_rewrite) . $sercuritySalt);
		$category = $this->Category->getCategoryId($cat_rewrite);
		$conditions = array(
				'active' => 1,
				'del_flg' => 0,
				'category_id' => $category['list_id'],
		);
		$paginate = array(
				'fields' => 'News.id, News.category_id, News.name, News.brief, News.new_rewrite,News.new_picture, Category.cat_name, Category.cat_parent_rewrite, Category.cat_rewrite, News.last_update',
				'limit' => ConstAdminComponent::PAGE_FRONTEND_LIMIT,
				'joins' => array(
					array(
						'table' => 'categories_multi',
						'alias' => 'Category',
						'type' => 'INNER',
						'conditions' => '`Category`.`id` = `News`.`category_id`'
					)
				),
				'conditions' => $conditions,
				'order' => array(
					'last_update' => 'DESC',
				),
		);

		$this->Paginator->settings = $paginate;
		$listNews = $this->Paginator->paginate('News');

		$title_for_layout = $category['category']['categories_multi']['cat_name'];
		$keywords_for_layout = $category['category']['categories_multi']['cat_keyword'];
		$description_for_layout = $category['category']['categories_multi']['cat_description'];

		$this->set(
			array(
				'cat_name' => $category['category']['categories_multi']['cat_name'],
				'cat_rewrite' => $this->request->params['cat_rewrite'],
				'list_news' => $listNews,
				'page_number_limit' => ConstAdminComponent::PAGE_FRONTEND_LIMIT,
				'title_for_layout' => $title_for_layout,
				'keywords_for_layout' => $keywords_for_layout,
				'description_for_layout' => $description_for_layout,
			)
		);
	}

	/**
	 * Gửi câu hỏi
	 */
	public function sendQuestion() {
		$this->render(false);
		if ($this->request->is('post')) {
			$params = $this->request->data;
			if($this->Captcha->validateCaptcha()) {
				$this->Faqs->create();
				$faqParams = array();
				$faqParams['Faqs']['name'] = $params['name'];
				$faqParams['Faqs']['email'] = $params['email'];
				$faqParams['Faqs']['question'] = $params['question'];
				$faqParams['Faqs']['question_rewrite'] = CommonComponent::rewriteTitle($params['question']);
				$faqParams['Faqs']['active'] = 0;
				$faqParams['Faqs']['category_id'] = $params['iCat'];
				$faqParams['Faqs']['last_update'] = time();
				$faqParams['Faqs']['create_date'] = time();

				if ($this->Faqs->save($faqParams)) {
					//send mail
					if (!empty($this->company['Configuration']['gmail_name']) && !empty($this->company['Configuration']['gmail_pass'])) {
						try {
							$message = $params['question'];
							$Email = new CakeEmail($this->gmail);
							$Email->from(array($this->company['Configuration']['gmail_name'] => $this->company['Configuration']['name']))
							->emailFormat('html')
							->to($this->company['Configuration']['email'])
							->subject('Khách hàng gửi câu hỏi.')
							->send($message);
						} catch (Exception $ex) {
							CakeLog::error('Faq send mail error' . $ex->getMessage());
							continue;
						}
					}

					$this->Session->setFlash(__('Success'), 'success');
				} else {
					$this->Session->setFlash(__('Save could not be saved. Please, try again'), 'error');
				}
			} else {
				$this->Session->setFlash(__('Mã bảo mật không chính xác.'), 'error');
			}

			if (isset($params['iCat']) && !empty($params['iCat'])) {
				$this->redirect(array(
						'controller' => 'categorys',
						'action' => 'index',
						'cat_rewrite' => $params['cRewrite'],
						'cat_id' => $params['iCat'],
						'cat_parent_rewrite' => isset($params['cpRewrite']) ? $params['cpRewrite']: '',
					)
				);
			} else {
				$this->redirect(array(
						'controller' => 'news',
						'action' => 'detail',
						'cat_rewrite' => $params['cRewrite'],
						'news_id' => $params['iNews'],
						'news_rewrite' => isset($params['nRewrite']) ? $params['nRewrite'] : '',
					)
				);
			}
		}
	}

	/**
	 * Hien thi ma bao mat
	 */
	public function captcha()
	{
		$this->Captcha->configCaptcha(
				array(
						'width'=>200,//image width -default 200
						'height'=>70,//image height-default 70
						'quality'=>2,//set quality-high:3 ,mid:2 ,low:1
						'blur'=>false,//enablle/disable blur
						'format'=>'jpeg',//iage formats. jpeg or png
						'wordFile'=>false,//include word file or not
						'maxLength'=>5,//min. word length
						'minLength'=>5,//max word length
						//'background'=>array(87,87,87), //rgb color code here
						'color'=>array(
								array(128,0,255), //each color code in rgb in form of array
								array(0,255,255),
								array(255,0,0),
								//    true //uncomment if overwrite existing colors,comment if append new colors to existing ones
						),
						'pathType'=> 2 //default:1.The Alternate path type    1 or 2.change pathtype to 2 if 1 does not work.
				)
		);

		$this->Captcha->getCaptcha();
	}

}
